#include "CSpider.h"
#include "..\Wrappers\CSGD_Direct3D.h"
#include "CCamera.h"
#include <stdio.h>
#include "..\CAnimationManager.h"

CSpider::CSpider(void)
{
	SetType(CBase::OBJ_SPIDER);
	SetWidth(50);
	SetHeight(50);
	speed = 50;
	damage = 35;
	stopped = false;
	totaltime = 0.0f;

	

	SpiderDownAnimation = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/SpiderDown.xml",D3DCOLOR_XRGB(255,0,255));
	SpiderLeftAnimation = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/SpiderLeft.xml",D3DCOLOR_XRGB(255,0,255));
	SpiderUpAnimation = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/SpiderUp.xml",D3DCOLOR_XRGB(255,0,255));
}

CSpider::~CSpider(void)
{

}

void CSpider::Fire(void)
{

}

void CSpider::Update(float fElapsedTime)
{
	if(stopped)
	{
		totaltime += fElapsedTime;
		if(totaltime >= 1.5f)
		{
			stopped = false;
			totaltime = 0.0f;
		}
		return;
	}
	CEnemy::Update(fElapsedTime);
	tVector2D tTemp;
	tTemp.fX = CPlayer::GetInstance()->GetPosX() - GetPosX();
	tTemp.fY = CPlayer::GetInstance()->GetPosY() - GetPosY();
	tTemp = Vector2DNormalize(tTemp);
	SetVelX(tTemp.fX*speed);
	SetVelY(tTemp.fY*speed);



	if(tTemp.fX <0 && tTemp.fY <0.7 && tTemp.fY > -0.7)
	{
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, SpiderLeftAnimation);
		left = true;
	}
	else
		left = false;

	if(tTemp.fX >0 && tTemp.fY <0.7 && tTemp.fY > -0.7)
	{
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, SpiderLeftAnimation);
		right = true;
	}
	else 
		right = false;
	if(tTemp.fY > 0 && tTemp.fX <0.7 && tTemp.fX > -0.7)
	{
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, SpiderDownAnimation);
		down = true;
	}
	else 
		down = false;

	if(tTemp.fY < 0 && tTemp.fX <0.7 && tTemp.fX > -0.7)
	{
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, SpiderUpAnimation);
		up = true;
	}
	else
		up = false;

	
}

void CSpider::Render(void)
{

	RECT drawRect = GetCollisionRect();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	// Show collision box

	//CSGD_Direct3D::GetInstance()->DrawRect(drawRect, 1, 1, 1);
	// Draw health above enemies head
	//char buffer[128];
	//sprintf_s(buffer, _countof(buffer), "HP: %i", GetHealth());

	if(right )
	{
		CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left),drawRect.top,-0.5f,1.0f, SpiderLeftAnimation);		
	}
	else if(left)
	{
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,0.5f,0.5f, SpiderLeftAnimation);
	}
	else if(down)
	{
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,0.5f,0.5f, SpiderDownAnimation);		
	}
	else if(up)
	{
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,0.5f,0.5f, SpiderUpAnimation);	
	}
	
	//CSGD_Direct3D::GetInstance()->DrawText(buffer, (int)GetPosX(), (int)GetPosY() - 20, 255, 0, 0);	
}

bool CSpider::CheckCollision(IBaseInterface* pBase)
{
	if(CEnemy::CheckCollision(pBase))
		return true;

	return false;
}